<template>
	<ynet-content id="sysAuth" :headerInformation="true" :headerMenu="true">
        <div class="main-container">
			<ynet-breadcrumb></ynet-breadcrumb>
		</div>   
		<div class="main-container" >
            <template v-if="temp2">
                <div class="fourForm tableForm mt20">
                    <h3>Pending Task(s)</h3>
                    <el-table :data="bsnAuthList" border @selection-change="handleChange">
                        <el-table-column label="选择" type="selection" ></el-table-column>
                        <el-table-column label="Transaction Sequence No." prop="flowNo" align="center">
                            <template scope="scope">
                                <el-button type="text" @click="openDetail(scope.$index,scope.row)"><p class="cBlue">{{scope.row.flowNo}}</p></el-button>
                            </template>
                        </el-table-column>  
                        <el-table-column label="Transaction Type" prop="bsnName" align="center"></el-table-column>  
                        <el-table-column label="Action" prop="actionFlag" align="center">
                            <template scope="scope">
                                {{scope.row.actionType==0?"Add":scope.row.actionType==1?"Modify":scope.row.actionType==2?"Delete":""}}
                            </template>
                        </el-table-column>
                        <el-table-column label="Submit" prop="userAlias" align="center"></el-table-column>  
                        <el-table-column label="Submission Time" prop="submitTime" align="center">
                            <template scope="scope">
                                {{$tools.utils.formatDate(scope.row.submitTime,{"symbol":"YYYY-MM-DD HH:mm"})}}
                            </template>
                        </el-table-column>  
                    </el-table>
                </div>
                <div class="pagination tar mt20">
                    <el-pagination  @current-change="handleCurrentChange" :current-page="Number(queryForm.currentPage)" :page-size="Number(queryForm.turnPageShowNum)" layout="total, prev, pager, next, jumper" :total="Number(turnPageTotalNum)">
                    </el-pagination>
                </div>
                <div class="btnArea mt20 tac">
                    <el-button type="primary" @click="agree()">Approve</el-button>
                    <el-button type="primary" @click="rules()">Reject</el-button>
                </div>
            </template>
            <!--拒绝页面弹窗-->
            <el-dialog :visible.sync="dialogTableVisible" :close-on-click-modal="false" :close-on-press-escape="false">
                <el-form ref="rejectForm" :model="rejectForm" class="formEmbedTable">
                    <table>
                        <caption>Refusal reason</caption>
                        <tr>
                            <td>Refusal reason</td>
                            <td>
                                <el-form-item prop="rejReason">
                                    <el-input placeholder="Refusal reason" v-model="rejectForm.rejReason"></el-input>
                                </el-form-item>
                            </td>
                        </tr>
                    </table>
                    <div class="btnArea mt20 tac">
                        <el-button type="primary" @click="dialogTableVisible = false,reject()">Confirm</el-button>
                    </div>
                </el-form>
            </el-dialog>
            <!--拒绝页面弹窗end-->
            <div>
                <!--详情页--> <!-- -->
                <RoleDetail v-if="IM0102" :detailData="detailData" :toDetail="toDetail" :oldDetailData="oldDetailData" @back="back"></RoleDetail> <!-- 角色详情页-->
                <BranchDetail v-if="IM0101" :detailData="detailData" :toDetail="toDetail" :oldDetailData="oldDetailData" @back="back"></BranchDetail> <!-- 机构管理-->
                <UserDetail v-if="IM0103" :detailData="detailData" :toDetail="toDetail" :oldDetailData="oldDetailData" @back="back"></UserDetail><!-- 操作员管理 -->
                <AuthModelDetail v-if="IM0104" :detailData="detailData" :toDetail="toDetail" :oldDetailData="oldDetailData" @back="back"></AuthModelDetail> <!-- 授权模式-->
            </div>
		</div>
	</ynet-content>
</template>

<script>
import RoleDetail from "./components/IM0102Detail";
import BranchDetail from "./components/IM0101Detail";
import UserDetail from "./components/IM0103Detail";
import AuthModelDetail from "./components/IM0104Detail";

export default {
  name: "sysAuth",
  components: {
    BranchDetail,
    RoleDetail,
    UserDetail,
    AuthModelDetail
  },

  data() {
    return {
      dialogTableVisible: false,
      turnPageTotalNum: "",
      IM0102: false,
      IM0101: false,
      IM0103: false,
      IM0104: false,
      IM0107: false,
      IM0202: false,
      IM0106: false,
      IM0105: false,
      IM0203: false,
      IM0204: false,
      IM0205: false,
      IM060501: false,
      authList: [],
      bsnAuthList: [], //某一菜单下待审核详情
      authInfoList: [], //待审核详情
      selectData: [],
      temp2: true,
      bsnCode: "", //菜单编号
      rejectForm: {
        //审核拒绝列表
        bsnAuthList: [],
        authFlag: "1",
        rejReason: ""
      },
      agreeForm: {
        bsnAuthList: [],
        authFlag: "0"
      },
      detailData: {}, //传给详情页的对象
      oldDetailData: {}, //修改前的数据
      toDetail: {}, //传给详情页用来提交给后台的数据
      queryForm: {
        uerName: "",
        currentPage: "1",
        turnPageShowNum: "10",
        bsnCode: ""
      }
    };
  },
  mounted() {
    //从后台查询该用户待审核业务列表
    //从缓存里取操作员登录员编号
    //let user = this.$tools.storage.get("user", "save");
    //console.log("登录页浏览器缓存" + JSON.stringify(user))
    //this.queryForm.userNo = "0000001";
    //console.log("queryForm:" + JSON.stringify(this.queryForm));
    this.queryAuth();
  },
  methods: {
    handleCurrentChange(val) {
      //分页方法
      this.queryForm.currentPage = val;
      this.queryAuth();
    },
    queryAuth() {
      //打开某个单独菜单下的待审核信息
      //console.log("queryForm:"+JSON.stringify(this.queryForm))
      this.$tools
        .request(this, "IM0301QueryAction.do", this.queryForm)
        .then(data => {
          //console.log("待审核信息:"+JSON.stringify(data))
          if (data.body.errorCode == 0) {
            this.bsnAuthList = data.body.bsnAuthList;
            this.turnPageTotalNum = data.body.turnPageTotalNum;
          } else {
            this.$alert("System Error Try again", "warning", {
              confirmButtonText: "Confirm"
            });
          }
        });
    },
    handleChange(newRow) {
      //console.log(JSON.stringify(newRow))
      this.selectData = newRow;
      //console.log("selectData:"+JSON.stringify(this.selectData))
    },
    back() {
      //返回审核页
      this.temp2 = true;
      this.IM0101 = false;
      this.IM0102 = false;
      this.IM0103 = false;
      this.IM0104 = false;
      //刷新审核页面
      this.queryAuth();
    },
    openDetail(index, row) {
      //显示详情页
      var jsonData;
      var jsonOldData;
      let bsnCode = row.bsnCode;
      this.temp2 = false;
      this.toDetail = $.extend(this.toDetail, row);
      //console.log("详情页toDetail:"+JSON.stringify(this.toDetail))
      for (let i = 0; i < this.bsnAuthList.length; i++) {
        if (this.bsnAuthList[i].flowNo == row.flowNo) {
          jsonData = this.bsnAuthList[i].jsonStr; //json字符串
          jsonOldData = this.bsnAuthList[i].jsonStrOld;
          this.detailData = this.$tools.utils.transferJson(jsonData);
          console.log(jsonOldData.length)
          if(jsonOldData){
            let midData = this.$tools.utils.transferJson(jsonOldData);
            console.log("详情页具体:midData:"+JSON.stringify(midData))
            if (bsnCode == "IM0101") {
              this.oldDetailData = midData.branchInfoList[0];
            }
            if (bsnCode == "IM0104") {
              this.oldDetailData = midData.imBsnAuthList[0];
            }
            if (bsnCode == "IM0103") {
              this.oldDetailData = midData.imUserList[0];
            }
            if (bsnCode == "IM0102") {
              this.oldDetailData = midData;
            }
          }else{
            this.oldDetailData=""
          }
          console.log("详情页具体:oldDetailData:"+JSON.stringify(this.oldDetailData))
          break;
        }
      }
      switch (bsnCode) {
        case "IM0102": //角色详情
          return (this.IM0102 = true);
        case "IM0101": //角色详情
          return (this.IM0101 = true);
        case "IM0103": //角色详情
          return (this.IM0103 = true);
        case "IM0104": //角色详情
          return (this.IM0104 = true);
      }
    },
    agree() {
      //判断是否选中数据
      if (this.selectData.length == 0) {
        this.$alert("select at least one", {
          confirmButtonText: "Confirm"
        });
      } else {
        this.agreeForm.bsnAuthList = this.selectData;
        //console.log("selectData"+JSON.stringify(this.selectData))
        console.log("agreeForm:" + JSON.stringify(this.agreeForm));
        for (let i = 0; i < this.agreeForm.bsnAuthList.length; i++) {
          this.$tools
            .request(
              this,
              this.agreeForm.bsnAuthList[0].bsnCode + "AuthAction.do",
              this.agreeForm
            )
            .then(data => {
              //console.log("data"+JSON.stringify(data))
              if (data.body.errorCode == 0) {
                this.$message({
                  type: "success",
                  message: "Success",
                  showClose: true
                });
                this.queryAuth();
              } else {
                this.$alert("System Error Try again", "warning", {
                  confirmButtonText: "Confirm"
                });
              }
            });
        }
      }
    },
    rules() {
      //判断是否选中数据
      if (this.selectData.length == 0) {
        this.$alert("select at least one", {
          confirmButtonText: "Confirm",
          callback: action => {}
        });
      } else {
        this.dialogTableVisible = true;
      }
    },
    reject() {
      this.rejectForm.bsnAuthList = this.selectData;
      //console.log("rejectForm:"+JSON.stringify(this.rejectForm))
      this.$tools.request(this,this.rejectForm.bsnAuthList[0].bsnCode + "AuthAction.do",this.rejectForm).then(data => {
        if (data.body.errorCode == 0) {
          this.$message({
            type: "success",
            message: "Success",
            showClose: true
          });
          this.queryAuth();
        } else {
          this.$alert("System Error Try again", "warning", {
            confirmButtonText: "Confirm"
          });
        }
      });
    }
  }
};
</script>